package data;

import java.util.ArrayList;
import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import models.NewsModel;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class NewsData {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "";
	
	private int limit = 20;
	
	private int page = 1;

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	public NewsData() {
		try {
			ObjectifyService.register(NewsModel.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public ArrayList<NewsModel> list() {
		ArrayList<NewsModel> listNews = new ArrayList<NewsModel>();
		
		int start = (page - 1) * limit;
		Query<NewsModel> q = ofy.query(NewsModel.class).filter("published", true).filter("created !=", "null").order("-created").limit(limit).offset(start);
		for (NewsModel news: q) {
			listNews.add(news);
		}
		
		return listNews;
	}
	
	public ArrayList<NewsModel> listByCreator(String username) {
		ArrayList<NewsModel> listNews = new ArrayList<NewsModel>();
		
		int start = (page - 1) * limit;
		Query<NewsModel> q = ofy.query(NewsModel.class).filter("creator", username).filter("created !=", "null").order("-created").limit(limit).offset(start);
		for (NewsModel news: q) {
			listNews.add(news);
		}
		
		return listNews;
	}
	
	public ArrayList<NewsModel> getHotNews() {
		ArrayList<NewsModel> listNews = new ArrayList<NewsModel>();
		
		Query<NewsModel> q = ofy.query(NewsModel.class).filter("published", true).filter("hotNews", true).filter("created !=", "null").order("-created").limit(10).offset(0);
		for (NewsModel news: q) {
			listNews.add(news);
		}
		
		return listNews;
	}
	
	public ArrayList<NewsModel> listHot() {
		ArrayList<NewsModel> listNews = new ArrayList<NewsModel>();
		
		int start = (page - 1) * limit;
		Query<NewsModel> q = ofy.query(NewsModel.class).filter("published", true).filter("hotNews", true).filter("created !=", "null").order("-created").limit(limit).offset(start);
		for (NewsModel news: q) {
			listNews.add(news);
		}
		
		return listNews;
	}
	
	public void insert(NewsModel news) {
		if (cache != null) {
			cache.put(prefix + news.getId(), news);
		}
		ofy.put(news);
	}
	
	public void delete(NewsModel news) {
		if (cache != null && cache.containsKey(prefix + news.getId())) {
			cache.remove(prefix + news.getId());
		}
		
		ofy.delete(news);
	}
	
	public NewsModel getNews(String id) {
		NewsModel news = new NewsModel();
		if (cache != null && cache.containsKey(prefix + id)) {
			news = (NewsModel) cache.get(prefix + id);
		} else {
			try {
				news = ofy.get(new Key<NewsModel>(NewsModel.class, id));
				cache.put(prefix + id, news);
			} catch (Exception e) {
				news = null;
			}
		}
		return news;
	}
	
	public boolean checkNewsExists(String url, String autoSite) {
		boolean exists = false;
		NewsModel news = (NewsModel) ofy.query(NewsModel.class).filter("url", url).filter("autoSite", autoSite).get();
		if (news != null) {
			exists = true;
		}
		return exists;
	}
}
